//
//  RefreshCarCondition.m
//  carcareIOS
//
//  Created by wr on 16/3/9.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "RefreshCarCondition.h"
#import "NetCarFence.h"
#import "CarFenceMgr.h"
#import "FuelMgr.h"
#import "NetSetting.h"
#import "SettingMgr.h"

@implementation RefreshCarCondition

IMP_SINGLETON

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.timeInterval = 10000;
        __weak __typeof(self) wself = self;
        self.Refresh = ^()
        {
            [[NetCarFence carFenceDatas]requestSuccess:^(id JSON) {
                NSArray *tmp = JSON[@"list"];
                NSMutableArray *models = [NSMutableArray arrayWithCapacity:tmp.count];
                for (int i = 0; i < tmp.count; i++) {
                    CarFenceModel *model = [[CarFenceModel alloc] initWithDic:tmp[i]];
                    [models addObject:model];
                }
                [CarFenceMgr sharedInstance].fences = models;
                if ([CarFenceMgr sharedInstance].fences.count > 0 ) {
                    CarFenceModel * model = [[CarFenceMgr sharedInstance].fences lastObject];
                    wself.carFenceMgrRadius = [NSString stringWithFormat:@"范围:%zd米", model.radius];
                    if(wself.RefreshCallBack)wself.RefreshCallBack([CarFenceMgr sharedInstance].fences);
                }else{
                    wself.carFenceMgrRadius = @"马上设置";
                }
            } failure:^(id JSON) {
                
            }];
            
            [[FuelMgr sharedInstance] getRecentlyFuelDate:^(NSString *date) {
                if (![date isNotNil]) {
                    wself.oilMgrLastTime = @"最近无加油";
                }else{
                    wself.oilMgrLastTime = [NSString stringWithFormat:@"上次加油:%@",date];
                }
                if (wself.RefreshCallBack) wself.RefreshCallBack(nil);
            }];
 
            [[NetSetting currentVoltageValue] requestSuccess:^(id JSON) {
                
                if(SETTINGMGR.voltageValue != 0)
                {
                    CGFloat currentVoltage = [JSON[@"curVoltage"] floatValue];//当前电压
                    wself.volatileSettingState = [NSString stringWithFormat:@"状态:%@",[NSString stringWithFormat:(SETTINGMGR.showTipsVoltage ? (currentVoltage < SETTINGMGR.voltageValue ? @"低" : @"正常") : @"关闭")]];
                    if (wself.RefreshCallBack) {
                        wself.RefreshCallBack(JSON);
                    }
                }
                
            }];

        };
    }
    return self;
}

@end
